/*
 * Copyright (c) [2023] Huawei Technologies Co.,Ltd.All rights reserved.
 *
 * OpenArkCompiler is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
 * FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#ifndef MPL2MPL_INCLUDE_MERGE_THE_SAME_CALLS_H
#define MPL2MPL_INCLUDE_MERGE_THE_SAME_CALLS_H

#include "mir_module.h"
#include "maple_phase_manager.h"

namespace maple {
class MergeTheSameCalls {
 public:
  explicit MergeTheSameCalls(MIRModule &module) : mod(module) {};

  virtual ~MergeTheSameCalls() = default;

  void Run();

 private:
  void ProcessBlock(BlockNode &block);
  StmtNode *ProcessStmt(BlockNode &block, StmtNode &stmt);
  StmtNode *DealWithCall(BlockNode &block, StmtNode &stmt);
  bool IsTheSameOpnds(const StmtNode &stmt1, const StmtNode &stmt2) const;
  bool IsTheSameOpnd(const BaseNode &opnd1, const BaseNode &opnd2,
      const CallReturnVector &vec1, const CallReturnVector &vec2) const;
  bool IsTheSameStmts(const StmtNode &stmt1, const StmtNode &stmt2,
      const CallReturnVector &vec1, const CallReturnVector &vec2) const;
  bool IsTheSameDass(const StmtNode &stmt1, const StmtNode &stmt2,
      const CallReturnVector &vec1, const CallReturnVector &vec2) const;
  bool StmtListIsEqual(const StmtNode *stmt1, const StmtNode *stmt2,
      const CallReturnVector &vec1, const CallReturnVector &vec2) const;

  MIRModule &mod;
  MIRFunction *currFunc = nullptr;
  // Record the callnodes that has been traversed.
  std::unordered_map<PUIdx, std::unordered_map<CallNode*, BlockNode*>> callNodeCands;
};

MAPLE_MODULE_PHASE_DECLARE(M2MMergeTheSameCalls)
}  // namespace maple
#endif  // MPL2MPL_INCLUDE_MERGE_THE_SAME_CALLS_H